from typing import List
from utils import combine, format

def hmm_val(V: List, pi: List, A: List, B: List, combine) -> float:
    '''
    HMM估值
    输入 观察序列 HMM模型参数pi,A,B 序列组合函数
    返回 HMM每种状态序列下输出对应观察序列的概率
    '''
    # 获得状态号集合
    W = [i for i in range(len(pi))]
    
    # 获得所有可能的状态序列
    WS = combine(W,len(V))

    # 计算观察值对应各状态序列出现的概率
    pw = []
    for ws in WS:
        p = pi[ws[0]]
        for i,w in enumerate(ws[1:]):
            p *= A[ws[i]][w] 

        for i,v in enumerate(V):
            p *= B[ws[i]][v]
        print("状态序列:", format(ws), "概率:", p)
        pw.append(p)
    return pw


# for debug
# V = [0, 2, 1]

# pi = [0.5, 0.3, 0.1, 0.1]

# A = [
#         [0.5, 0.3, 0.1, 0.1],
#         [0.3, 0.3, 0.3, 0.1],
#         [0.4, 0.2, 0.3, 0.1],
#         [0.5, 0.2, 0.1, 0.2]

#     ]

# B = [
#         [0.5, 0.4, 0.1],
#         [0.3, 0.4, 0.3],
#         [0.1, 0.3, 0.6],
#         [0.1, 0.2, 0.7]
#     ]


# hmm = hmm_val(V,pi,A,B,combine)